//
//  ChooseIdCardTypeCell.swift
//  选择证件类型 cell
//
//  Created by 千策万划-mini on 2025/5/2.
//

import Foundation

class ChooseIdCardTypeCell: UITableViewCell {
    
    // MARK: - block
    var blockItemClick: ((ChooseIdCardTypeCell) -> Void)?
    
    // MARK: - lazy
    lazy var backView: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(SCREEN_WIDTH-2*W(15), W(52))
        result.isUserInteractionEnabled = true
        result.setBackgroundImage(UIImage(named: "icon_KYC_section_item_bg"), for: .normal)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelTitle: UILabel = {
        let result = UILabel()
        result.textColor = COLOR_LABEL()
        result.font = UIFont.customSemiBoldFont(size: 16)
        return result
    }()
    lazy var btnState: UIButton = {
        let result = UIButton()
        result.setBackgroundImage(UIImage(named: "icon_agree_uncheck"), for: .normal)
        result.setBackgroundImage(UIImage(named: "icon_agree_check"), for: .selected)
        result.widthHeight = XY(W(24), W(24))
        result.isUserInteractionEnabled = false
        return result
    }()
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = .clear
        self.backgroundColor = .clear
        self.selectionStyle = .none
        setUpView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpView() {
        contentView.addSubview(backView)
        backView.addSubview(labelTitle)
        backView.addSubview(btnState)
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        if let block = blockItemClick {
            block(self)
        }
    }
    
    // MARK: - 重置界面
    @objc func resetCellWithModel(_ result: Any){
        guard let model = result as? ModelBtn else { return }
        
        backView.centerXTop = XY(SCREEN_WIDTH/2, W(10))
        
        labelTitle.fitTitle(UnPackStr(model.title), variable: 0)
        labelTitle.leftCenterY = XY(20, backView.height/2)
        
        btnState.isSelected = model.isSelected ?? false
        btnState.rightCenterY = XY(backView.width-labelTitle.left, labelTitle.centerY)
        
        height = backView.bottom
    }
}
